package com.shycloud.mido.member.controller;


import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.MiniBanner;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.api.vo.MiniBannerVO;
import com.shycloud.mido.member.service.MiniBannerService;
import com.shycloud.mido.member.service.MiniRewardRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import java.util.List;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 小程序banner管理 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2021-06-18
 */
@RequestMapping("/mini-banner")
@RestController
@AllArgsConstructor
@Api(tags = "小程序Banner管理")
@ApiIgnore
public class MiniBannerController {

	/** 小程序首页 banner 业务 service */
	private final MiniBannerService miniBannerService;
	/** 小程序首页领取奖励业务 service */
	private final MiniRewardRecordService rewardRecordService;

	@ApiOperation("后台-Banner分页查询")
	@GetMapping("/pageData")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "size", value = "每页显示条数", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "categoryId", value = "1首页2作业3比赛", paramType = "query", dataType = "string", required = true),
			@ApiImplicitParam(name = "title", value = "Banner标题", paramType = "query", dataType = "string", required = false)
	})
	public R<IPage<MiniBanner>> pageData(Long current, Long size, MiniBannerVO vo) throws Exception {
		IPage<MiniBanner> page = new Page<>(current, size);
		LambdaQueryWrapper<MiniBanner> pageQW = Wrappers.<MiniBanner>lambdaQuery().eq(MiniBanner::getDelFlag, Constant.DEL_FLAG)
				.eq(MiniBanner::getCategoryId, vo.getCategoryId());
		if (StringUtils.isNotBlank(vo.getTitle())) {
			pageQW.like(MiniBanner::getTitle, vo.getTitle());
		}
		pageQW.orderByDesc(MiniBanner::getUpdateTime);
		IPage<MiniBanner> result = miniBannerService.page(page, pageQW);
		return new R<IPage<MiniBanner>>(result, "操作成功");
	}


	@ApiOperation("后台-保存或更新Banner信息")
	@PostMapping("/saveOrUpdate")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "imgUrl", value = "图片URL", paramType = "query", dataType = "string", required = true),
			@ApiImplicitParam(name = "link", value = "链接", paramType = "query", dataType = "string", required = false),
			@ApiImplicitParam(name = "linkMethod", value = "跳转方式 : 1外部2内部3无", paramType = "query", dataType = "string", required = false),
			@ApiImplicitParam(name = "title", value = "Banner标题", paramType = "query", dataType = "string", required = false)
	})
	public R<Boolean> saveOrUpdate(@RequestBody MiniBannerVO vo) throws Exception {
		miniBannerService.saveOrUpdateMiniBanner(vo);
		return new R<Boolean>(true, "操作成功");
	}

	@ApiOperation("小程序-获取 Banner 信息")
	@GetMapping("/getMiniBanner")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "categoryId", value = "banner 类型:1首页2作业3比赛4精彩活动5免费资料6宣传(2)7悬浮分享8预设模板", paramType = "query", dataType = "string", required = true)
	})
	public R<List<MiniBanner>> getMiniBanner(String categoryId) throws Exception {
		return new R(miniBannerService.list(new QueryWrapper<MiniBanner>()
				.eq("category_id", categoryId).eq("status", "0").eq("del_flag", "0")));
	}

	@ApiOperation("小程序-领取奖励")
	@GetMapping("/receive")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "bannerId", value = "bannerId", paramType = "query", dataType = "string", required = true)
	})
	public R receive(String bannerId) {
		return new R(rewardRecordService.receive(bannerId));
	}

	@ApiOperation("小程序-判断是否领取奖励")
	@GetMapping("/checkReceive")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "bannerId", value = "bannerId", paramType = "query", dataType = "string", required = true)
	})
	public R checkReceive(String bannerId) {
		return new R(rewardRecordService.checkReceive(bannerId));
	}


	@ApiOperation("后台-删除Banner")
	@PostMapping("/delete")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "ID集合", paramType = "query", dataType = "String", required = true)
	})
	public R<Boolean> delete(@RequestBody List<String> ids) throws Exception {
		if (CollectionUtils.isEmpty(ids)) {
			return new R<Boolean>(false, "BannerID必填");
		}
		miniBannerService.removeByIds(ids);
		return new R<Boolean>(true, "操作成功");
	}

	@ApiOperation("后台-编辑状态")
	@GetMapping("/editStatus")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "ID", paramType = "query", dataType = "String", required = true),
			@ApiImplicitParam(name = "status", value = "状态 : 0启用 1禁用", paramType = "query", dataType = "String", required = true)
	})
	public R<Boolean> editStatus(MiniBannerVO vo) throws Exception {
		if (StringUtils.isBlank(vo.getId())) {
			return new R<Boolean>(false, "id必填");
		}
		miniBannerService.editStatus(vo);
		return new R<Boolean>(true, "操作成功");
	}


}
